package com.open.launcher.fragment;

import android.content.Context;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.open.launcher.R;
import com.open.library.network.EthernetManagerRef;
import com.open.library.network.NetWorkTools;
import com.open.widget.utils.ViewUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnFocusChange;
import butterknife.Unbinder;


/**
 * Created by ghq on 2018/4/6.
 */

public class NetSettingsFragment extends BaseFragment {

    @BindView(R.id.tv_netIpAddress)
    EditText tvNetIpAddress;
    @BindView(R.id.tv_subNetMask)
    EditText tvSubNetMask;
    @BindView(R.id.tv_gateway)
    EditText tvGateway;
    @BindView(R.id.tv_dns)
    EditText tvDns;

    Unbinder unbinder;
    EthernetManagerRef mEthernetManagerRef;
//    EthernetManager ethernetManager;
    private NetWorkTools mNetWorkTools;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mEthernetManagerRef = new EthernetManagerRef(getContext());
//        getActivity().getSystemService(ETHERNET_SERVICE)
////        mEthManager = (EthernetManager) getActivity().getSystemService(Context.ETHERNET_SERVICE);
//        EthernetManager ethernetManager =
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_settings_net, null);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tvNetIpAddress.requestFocus();
        setNetInfoShow();
    }

    private void setNetInfoShow() {
        mNetWorkTools = new NetWorkTools(getActivity());
        String ipAddr = mNetWorkTools.getIpAddr();
        if (!TextUtils.isEmpty(ipAddr)) {
            tvNetIpAddress.setText(ipAddr);
        }else {
            tvNetIpAddress.setText("暂未获取到有线网络信息");
        }
        String netMask = mNetWorkTools.getNetMask();
        if (!TextUtils.isEmpty(netMask)) {
            tvSubNetMask.setText(netMask);
        }else {
            tvSubNetMask.setText("暂未获取到有线网络信息");

        }
        String gateWay = mNetWorkTools.getDefaultWay();
        if (!TextUtils.isEmpty(gateWay)) {
            tvGateway.setText(gateWay);
        }else {
            tvGateway.setText("暂未获取到有线网络信息");
        }
        String dns = mNetWorkTools.getFirstDns();
        if (!TextUtils.isEmpty(dns)) {
            tvDns.setText(dns);
        }else {
            tvDns.setText("暂未获取到有线网络信息");
        }

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mEthernetManagerRef.setIp(tvNetIpAddress.getText().toString().trim());
        mEthernetManagerRef.setNetmask(tvSubNetMask.getText().toString().trim());
        mEthernetManagerRef.setDefaultWay(tvGateway.getText().toString().trim());
        mEthernetManagerRef.setFirstDns(tvDns.getText().toString().trim());
        unbinder.unbind();
    }

    @OnFocusChange({R.id.tv_netIpAddress,R.id.tv_subNetMask,R.id.tv_gateway,R.id.tv_dns})
    public void onViewFocusChange(View view, boolean isFocus){
        ViewUtils.scaleView(view, isFocus);
    }

}
